from lxml import etree
from tool import *
import requests,re,base64,time,json
session= requests.session()
pat_pro=re.compile(r"var obj \= jQuery\.parseJSON\('(.*)'\);$",re.M)
patSizes=re.compile(r"P\.register\('twister-js-init-dpx-data', function\(\) \{.*?var dataToReturn = (\{.*?\});",re.S)

headers={
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
}
ck=None

def get_csrftoken(cot):

    el=etree.HTML(cot).xpath('//span[@id="nav-global-location-data-modal-action"]/@data-a-modal')

    gdata=json.loads(el[0])
    return gdata['ajaxHeaders']['anti-csrftoken-a2z']

def jx(cot,asin):

    map=pat_pro.search(cot)

    jstxt=map.group(1)
    sr= patSizes.search(cot)
    with open(f'Jsons/{asin}.json','w',encoding='utf-8') as f:
        f.write(jstxt)
    with open(f'Jsons/{asin}_size.json','w',encoding='utf-8') as f:
        f.write(sr.group(1))

def get_cot(asinID):
    global ck
    print(f'开始请求ASIN:{asinID}')
    url=f'https://www.amazon.com/dp/{asinID}?th=1&psc=1'
    if not ck:
        res= session.get(url,headers=headers)
    else:
        res= session.get(url,headers=headers,cookies=ck)
    cot=res.text
    print(f'ASIN:{asinID},请求长度:{len(cot)}')
    if res.status_code!=200:
        if res.status_code==404:
            print(f'{asinID}=>产品资源不存在,删除该pid')
        else:
            print(f'{asinID}=>状态码错误:{res.status_code}')
        return

    if len(cot) <20000:
        
        cot,ck=check_amazon_yzm(session,cot)

    elSeller=etree.HTML(cot).xpath('//a[contains(@href,"/gp/help/seller")]/@href')
    print(elSeller)
    print('Best Sellers Rank' in cot)
    print(f'内容:{len(cot)},ck:{ck}')

    with open(f'opopop.html','w',encoding='utf-8') as f:
        f.write(cot)
    #jx(cot,asinID)


with open('pro.html','r',encoding='utf-8') as f:

    cot=f.read()

jx(cot,'解析错误333')
